package org.jeecg.modules.front.pay;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.front.util.YinShengUtil;
import org.jeecg.modules.yinsheng.YinShengClient;
import org.jeecg.modules.yinsheng.command.base.HeadResponseCommand;
import org.jeecg.modules.yinsheng.command.base.MerchantHeadRequestCommand;
import org.jeecg.modules.yinsheng.command.scancode.FrontCodePayReqCommand;
import org.jeecg.modules.yinsheng.command.scancode.FrontCodePayRespCommand;
import org.jeecg.modules.yinsheng.common.ServiceEnum;
import org.jeecg.modules.yinsheng.common.YsPayConstant;
import org.jeecg.modules.yinsheng.utils.JsonParser;
import org.jeecg.modules.yinsheng.utils.YinshengLogger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class YinshengAggregationForwardScan extends Payment{


    public YinshengAggregationForwardScan(String channel) {
        // 构造方法体
        this.channel = channel;

    }

    public YinshengAggregationForwardScan() {
        // 构造方法体

    }

    public Result<JSONObject> pay(String requestNo, Double amount, Map<String, String> extendParams)
    {

        // 先构建请求客户端
        YinShengClient yinShengClient = new YinShengClient(ServiceEnum.WALLET_PAY_FRONT_CODE, YsPayConstant.ENV_PRD, YinShengUtil.getPrdMerchantPrx(), YinShengUtil.getPrdMerchantPrxPwd(), YinShengUtil.getRsaPrd());
        MerchantHeadRequestCommand headRequestCommand = new MerchantHeadRequestCommand().setSrcMerchantNo(YinShengUtil.getPrdSrcMerchantNo());
        headRequestCommand.setServiceNo(ServiceEnum.WALLET_PAY_FRONT_CODE.getApiName());
        FrontCodePayReqCommand requestCommand = new FrontCodePayReqCommand();
        //商户系统生成的订单号
        requestCommand.setRequestNo(requestNo);
        //收款方银盛商户号
        requestCommand.setPayeeMerchantNo(YinShengUtil.getPrdPayeeMerchantNo());
        //商品名称/订单标题
        requestCommand.setOrderDesc((extendParams.get("name")));
        //交易金额，单位：元
        requestCommand.setAmount(String.valueOf(amount));
        if(this.channel.equals("alipay")){
            //支付宝
            requestCommand.setBankType("1903000");
        }
        requestCommand.setNotifyUrl(YinShengUtil.getNotifyUrl()+"front/userHomeOrder/yinsheng/notify");
        requestCommand.setTimeOut("120");
        requestCommand.setTranType("01");
        requestCommand.setIsDivision("N");
        HeadResponseCommand baseResponseCommand = yinShengClient.doAction(headRequestCommand, requestCommand);
        YinshengLogger.logBizInfo(baseResponseCommand);
        FrontCodePayRespCommand respCommand = JsonParser
                .parserJsonStringToJavaObject(baseResponseCommand.getBizResponseJson(), FrontCodePayRespCommand.class);
        String str= JsonParser.parserJavaObjectToJsonString(respCommand, false);
        JSONObject jsonObject = JSON.parseObject(str);
        if(jsonObject.get("subCode").equals("COM000")||jsonObject.get("subCode").equals("COM004"))
        {
            return Result.ok(jsonObject);
        }
        else
        {
            return Result.error(String.valueOf(jsonObject.get("subMsg")));
        }

    }


    public  Integer payType()
    {
        if(this.channel.equals("alipay"))
        {
            return 5;
        }

        return 0;
    }
}
